package starcoast.bocai.com.starcoast.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.activitys.MessageActivity;
import starcoast.bocai.com.starcoast.activitys.ReserveMeetingActivity;
import starcoast.bocai.com.starcoast.activitys.SearchActivity;
import starcoast.bocai.com.starcoast.adapter.HomeActivityAdapter;
import starcoast.bocai.com.starcoast.adapter.HomeGirdViewAdp;
import starcoast.bocai.com.starcoast.bean.ADInfo;
import starcoast.bocai.com.starcoast.common.BaseFragment;
import starcoast.bocai.com.starcoast.util.ViewFactory;
import starcoast.bocai.com.starcoast.view.CycleViewPager;
import starcoast.bocai.com.starcoast.view.MyGridView;
import starcoast.bocai.com.starcoast.view.MyListView;

/**
 * Created by admin on 2016/6/3.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.tv_title)
    TextView mTitleTv;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.lv_home)
    MyListView lv_home;
    @Bind(R.id.home_preferential)
    MyGridView home_preferential;
    @Bind(R.id.message)
    ImageView message;
    @Bind(R.id.home_reserve)
    LinearLayout home_reserve;
    @Bind(R.id.home_scroll)
    ScrollView home_scroll;
    View view;
    HomeActivityAdapter homeActivityAdapter;
    HomeGirdViewAdp homeGirdViewAdp;
    CycleViewPager cycleViewPager;
    private List<ImageView> views = new ArrayList<ImageView>();
    private List<ADInfo> infos = new ArrayList<ADInfo>();
    private List<String> imageList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        initView();
        initOnclick();
        return view;
    }
    public void initView(){

        toolbar.inflateMenu(R.menu.menu_search);
        message.setVisibility(View.VISIBLE);
        message.setOnClickListener(this);
        imageList.add("http://img1.imgtn.bdimg.com/it/u=932695849,3126690420&fm=21&gp=0.jpg");
        imageList.add("http://img55.it168.com/ArticleImages/fnw/2016/0426/97642e6b-4b15-496e-b963-81f9d385c139.jpg");
        mTitleTv.setText(R.string.str_frag_home);

        homeActivityAdapter = new HomeActivityAdapter(getActivity());
        lv_home.setAdapter(homeActivityAdapter);
        lv_home.setFocusable(false);
        homeGirdViewAdp = new HomeGirdViewAdp(getActivity());
        home_preferential.setAdapter(homeGirdViewAdp);
        cycleViewPager = (CycleViewPager) getActivity().getFragmentManager()
                .findFragmentById(R.id.fragment_cycle_viewpager_content);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                        @Override
            public boolean onMenuItemClick(MenuItem item) {
                    startActivity(new Intent(getActivity(), SearchActivity.class));
                return false;
            }
        });
        configImageLoader();
        initialize();
        home_scroll.smoothScrollTo(0,0);
    }
    public void initOnclick(){
        home_reserve.setOnClickListener(this);
    }
    private void initialize() {
        for(int i = 0; i < imageList.size(); i ++){
            ADInfo info = new ADInfo();
            info.setUrl(imageList.get(i));
            info.setContent("图片-->" + i );
            infos.add(info);
        }
        // 将最后一个ImageView添加进来
        views.add(ViewFactory.getImageView(getActivity(), infos.get(infos.size() - 1).getUrl()));
        for (int i = 0; i < infos.size(); i++) {
            views.add(ViewFactory.getImageView(getActivity(), infos.get(i).getUrl()));
        }
        // 将第一个ImageView添加进来
        views.add(ViewFactory.getImageView(getActivity(), infos.get(0).getUrl()));

        // 设置循环，在调用setData方法前调用
        cycleViewPager.setCycle(true);

        // 在加载数据前设置是否循环
        cycleViewPager.setData(views, infos, mAdCycleViewListener);
        //设置轮播
        cycleViewPager.setWheel(true);

        // 设置轮播时间，默认5000ms
        cycleViewPager.setTime(2000);
        //设置圆点指示图标组居中显示，默认靠右
        cycleViewPager.setIndicatorCenter();
    }
    private CycleViewPager.ImageCycleViewListener mAdCycleViewListener = new CycleViewPager.ImageCycleViewListener() {

        @Override
        public void onImageClick(ADInfo info, int position, View imageView) {
            if (cycleViewPager.isCycle()) {

            }

        }

    };

    /**
     * 配置ImageLoder
     */
    private void configImageLoader() {
        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message:
                Intent intent = new Intent(getActivity(),MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.home_reserve:
                Intent intent1 = new Intent(getActivity(),ReserveMeetingActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void onResume() {
        Log.e("onResume","onResume");
        super.onResume();
    }
}
